package com.initialize.appapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initialize.appapi.service.CommentsService;
import com.initialize.pub.mybatis.plus.entity.Comments;
import com.initialize.pub.mybatis.plus.service.impl.CommentsServiceImpl;
import com.initialize.pub.token.UserLoginToken;
import com.initialize.pub.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/3/13
 * Time: 9:50 下午
 * Description:
 */

@Api(tags = "评论API")
@RestController
@CrossOrigin
@RequestMapping(value = "/comment")
public class CommentController {

    final
    CommentsServiceImpl commentsService;

    final
    CommentsService commentsServiceI;

    public CommentController(CommentsServiceImpl commentsService, CommentsService commentsServiceI) {
        this.commentsService = commentsService;
        this.commentsServiceI = commentsServiceI;
    }

    @ApiOperation("获取评论的数量")
    @PostMapping("/count")
    @UserLoginToken
    public R<Integer> getTheNumberOfReviews() {
        return R.ok(commentsService.count());
    }


    @ApiOperation("获取评论的列表")
    @GetMapping("/list/{size}")
    @UserLoginToken
    public R<Page<Comments>> getList(@PathVariable int size) {
        Page<Comments> page = commentsService.page(new Page<>(size, 10, true));
        Collection<Comments> records = page.getRecords();
        for (Comments r : records) {
            String content = r.getContent();
            String s = ToolUtil.removeMarkdown(content);
            if (s.length() > 50) {
                s = s.substring(0, 49) + "...";
            }
            r.setContent(s);
        }
        return R.ok(page);
    }

    @ApiOperation("获取首页的最新评论")
    @GetMapping("/new/{size}")
    @UserLoginToken
    public R<List<Comments>> getLatestComment(@PathVariable int size) {
        return R.ok(commentsServiceI.getLatestComment(size));
    }


    @ApiOperation("将评论关到小黑屋(不区分单个批量)")
    @PostMapping("/small_black_house")
    @UserLoginToken
    public R<String> closedIntoASmallBlackRoom(@RequestBody List<Integer> integerList) {
        integerList.forEach(o -> commentsService.update(commentsService.getById(o).setStatus(0), new QueryWrapper<Comments>().eq("cid", o)));
        return R.ok("成功");
    }

    @ApiOperation("将评论请出小黑屋(不区分单个批量)")
    @PostMapping("/please_out")
    @UserLoginToken
    public R<String> pleaseGoOut(@RequestBody List<Integer> integerList) {
        integerList.forEach(o -> commentsService.update(commentsService.getById(o).setStatus(1), new QueryWrapper<Comments>().eq("cid", o)));
        return R.ok("成功");
    }


    @ApiOperation("改变评论的状态(不区分单个批量)")
    @PostMapping("/change_state")
    @UserLoginToken
    public R<String> changeState(@RequestBody List<Integer> integerList) {
        integerList.forEach(o -> {
            Comments byId = commentsService.getById(o);
            commentsService.update(byId.setStatus(1 - byId.getStatus()), new QueryWrapper<Comments>().eq("cid", o));
        });
        return R.ok("成功");
    }


    @ApiOperation("删除评论(不区分单个批量)")
    @PostMapping("/delete")
    @UserLoginToken
    public R<Boolean> delete(@RequestBody List<Integer> integerList) {
        return R.ok(commentsService.removeByIds(integerList));
    }


}